# coding=utf-8
import requests
import json
import time
import pymysql
from config import db_host, db_port
import redis
# from playwright.sync_api import sync_playwright
import os
import asyncio
from playwright.async_api import async_playwright, Playwright

db = pymysql.connect(host="localhost",
                     user='root',
                     port=3306,
                     password='123456',
                     database='big_data')
# db_host = '121.4.69.146'
# db_port = 3306
# db = pymysql.connect(host=db_host,
#                      user='root',
#                      port=db_port,
#                      password='xhhg@2020',
#                      database='big_data')
cur = db.cursor()
redis_con = redis.Redis(host='127.0.0.1', port=6379,
                        db=2, decode_responses=True)


async def parse_patent_info(application_number, patent_status_res_json, inst):
    # 解析获取专利信息
    res_msg = patent_status_res_json.get('msg')
    if res_msg == '成功':
        patent_status_data = patent_status_res_json.get('data')
        item = dict()
        item['sqh'] = 'ZL' + application_number
        try:
            item["state"] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('anjianywzt')
        except:
            item["state"] = ''
        try:
            item["name"] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('zhuanlimc')
        except:
            item["name"] = ''
        try:
            item["sq_date"] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('shenqingr')
        except:
            item["sq_date"] = ''
        try:
            dljg_info = patent_status_data.get(
                'dailijg').get('dailijgList')[-1]
            item["dljg"] = dljg_info.get('dailijgdm')
            item["dlr"] = dljg_info.get('diyidlrxm')
        except:
            item["dljg"] = ''
            item["dlr"] = ''
        famingrenList = patent_status_data.get(
            'famingren').get('famingrenList')
        try:
            sqr_list = [line.get('famingrxm') for line in famingrenList]
        except:
            sqr_list = []
        try:
            shenqingrenList = patent_status_data.get(
                'shenqingren').get('shenqingrenList')
            item["sqr"] = ','.join([line.get('shenqingrxm')
                                    for line in shenqingrenList])
        except:
            item["sqr"] = ''
        item["fmr_name"] = ','.join(sqr_list)
        item['inst'] = inst
        try:
            item['fm_class'] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('zhufenlh')
        except:
            item['fm_class'] = ''
        try:
            item["fufenlei_hao"] = patent_status_data.get(
                'zhuluxmxx').get('zhuluxmxx').get('fufenlh')
        except:
            item["fufenlei_hao"] = ''
        try:
            lianxiren = patent_status_data.get(
                'lianxiren').get('lianxirenList')
            item["lianxiren"] = ','.join([line.get('lianxirxm')
                                          for line in lianxiren])
        except:
            item["fufenlei_hao"] = ''
        print(item)
        print('====' * 10)
        if inst == 'XHHG' and item["name"] == '--':
            try:
                redis_con.sadd("tmp_协和端口查询", application_number)
            except:
                page.wait_for_timeout(5000)
                redis_con.sadd("tmp_协和端口查询", application_number)
            # 使用协和端口查询
            print('@@xhhg未查到相关信息，使用xh进行查询@@')
        elif inst == 'XH' and item["name"] == '--':
            try:
                redis_con.sadd("tmp_外部接口查询", application_number)
            except:
                page.wait_for_timeout(5000)
                redis_con.sadd("tmp_外部接口查询", application_number)
            print('@@xh端口未查到相关信息，使用外部接口查询@@')
        elif inst == 'WB' and item["name"] == '--':
            try:
                redis_con.sadd("tmp_全部接口未查到", application_number)
            except:
                page.wait_for_timeout(5000)
                redis_con.sadd("tmp_全部接口未查到", application_number)
            print('@@全部接口均未查到@@')
        if item["name"] != '--' or inst == 'WB':
            sql = f"""INSERT INTO xhhg_pantent_status_0918 (application_number, case_status, fm_name,fm_class, sq_date, created_at, updated_at, agency, patent_user, first_agent, fmr, port,fufenlei_hao,lianxiren) VALUES
                ('{item["sqh"]}', '{item["state"]}', '{item["name"]}', '{item['fm_class']}', '{item["sq_date"]}', NOW(), NOW(), '{item["dljg"]}', '{item["sqr"]}', '{item["dlr"]}', '{item["fmr_name"]}', '{item["inst"]}','{item["fufenlei_hao"]}','{item["lianxiren"]}')"""
            print(sql)
            try:
                cur.execute(sql)
                db.commit()
            except Exception as e:
                print(e)
                print(f'{sqh}_插入数据库失败，5分钟后重试')
                page.wait_for_timeout(30000)
                cur.execute(sql)
                db.commit()


def select_sqh():
    sql = 'SELECT DISTINCT application_number FROM xhhg_pantent_status_0918'
    cur.execute(sql)
    data = cur.fetchall()
    have_already_done = set([sqh[0].replace("ZL", '') for sqh in data])
    return have_already_done


async def run(playwright, inst, port, have_already_done):
    # all_start_time = time.time()
    storage_state_path = f'./auth/{inst}_state.json'
    with open(storage_state_path) as file:
        storage_state = json.load(file)
        origins = storage_state.get('origins')
        local_stroge_list = [item.get('localStorage') for item in origins]
        for local_stroge in local_stroge_list:
            for item in local_stroge:
                if item.get('name') == 'ACCESS_TOKEN':
                    authorization = item.get('value')
    print(f'成功读取{inst}登录信息:{authorization}')
    browser = await playwright.firefox.launch()
    context = await browser.new_context(
        base_url='https://cpquery.cponline.cnipa.gov.cn', storage_state=storage_state_path)
    context.set_default_timeout(300000)
    context.set_default_navigation_timeout(300000)
    page = await context.new_page()
    await page.goto(
        'https://cpquery.cponline.cnipa.gov.cn/chinesepatent/index')
    # await page.wait_for_load_state('networkidle')
    scraped_nums = 1
    while redis_con.scard(port):
        sqh = redis_con.spop(port).replace("ZL", '').replace('.', '')
        scraped_nums += 1
        if sqh not in have_already_done:
            start_time = time.time()
            print(f'@@开始查询申请号:{sqh}_专利状态信息@@')
            api_request_context = context.request
            headers = {
                "Accept": "application/json, text/plain, */*",
                "Content-Type": "application/json;charset=utf-8",
                "Authorization": 'Bearer ' + authorization,
                "userType": "USER_RZ_DAILIJIG"
            }
            sqxx_response = await api_request_context.post(
                'https://cpquery.cponline.cnipa.gov.cn/api/view/gn/sqxx', data={'zhuanlisqh': sqh}, headers=headers, timeout=0)
            patent_status_res_json = await sqxx_response.json()
            # print(patent_status_res_json)
            await parse_patent_info(sqh, patent_status_res_json, inst)
            end_time = time.time()
            print(f"{sqh}_本次耗时: {end_time-start_time}")
            need_time = round((end_time - start_time) *
                              redis_con.scard(port) / 60, 1)
            print(
                f"已抓取专利数：{scraped_nums}，剩余{redis_con.scard(port)}个，大约还需要{need_time}分钟")
            await page.reload()
    # all_end_time = time.time()


async def main(inst, port, have_already_done):
    async with async_playwright() as playwright:
        await run(playwright, inst, port, have_already_done)

if __name__ == '__main__':
    have_already_done = select_sqh()
    print('@@读取已查询申请号@@')
    port_dict = {'XHHG': 'tmp_信合红谷接口查询',
                 'XH': 'tmp_协和端口查询', 'WB': "tmp_外部接口查询"}
    inst = 'XHHG'
    port = port_dict.get(inst)
    all_start_time = time.time()
    asyncio.run(main(inst, port, have_already_done))
    # run(inst, port, have_already_done)
    all_end_time = time.time()
    print(f"@@总耗时：{all_end_time - all_start_time}@@")
    db.close()
